<template>
  <div id="role-config">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" :model="formData" class="demo-form-inline" ref="form">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="formData.name" placeholder="角色名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">查询</el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button @click="handleCreate">新建角色</el-button>
      </div>
      <template>
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="编号" width="150">
          </el-table-column>
          <el-table-column prop="name" label="角色名称" width="180">
          </el-table-column>
          <el-table-column prop="description" label="描述">
          </el-table-column>
          <el-table-column prop="createdTime" label="添加时间">
          </el-table-column>
          <el-table-column label="操作" width="350">
            <template slot-scope="scope">
              <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
              <el-button size="mini" type="danger"
              @click="handleDelete(scope.row.id)">删除</el-button>
              <el-button size="mini"
              @click="$router.push({ name: 'allocmenu', params: {id:scope.row.id}})"
              >分配菜单</el-button>
              <el-button size="mini"
              @click="$router.push({ name: 'allocresource', params: {id:scope.row.id}})"
              >分配资源</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <el-dialog title="编辑角色" :visible.sync="dialogFormVisible">
        <el-form :model="EditForm">
          <el-form-item label="角色名称" :label-width="formLabelWidth">
            <el-input v-model="EditForm.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色编码" :label-width="formLabelWidth">
            <el-input v-model="EditForm.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="描述" :label-width="formLabelWidth">
            <el-input type="textarea" v-model="EditForm.description" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleEdited">确 定</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
// eslint-disable-next-line
import { GetAllRoles,GetRole,UpdateRole,DeleteRole } from '@/services/role';
// import router from '@/router';

export default {
  name: 'RoleConfig',
  created() {
    this.loadRoles();
  },
  data() {
    return {
      formData: {},
      tableData: [],
      EditForm: {},
      dialogFormVisible: false,
      formLabelWidth: '120px',
    };
  },
  methods: {
    async loadRoles() {
      const { data } = await GetAllRoles();
      if (data.code === '000000') {
        this.tableData = data.data;
      }
    },
    onReset() {
      this.$refs.form.resetFields();
    },
    handleCreate() {
      this.EditForm = {
        code: '',
        name: '',
        description: '',
      };
      this.dialogFormVisible = true;
    },
    async handleEdit(roleID) {
      const { data } = await GetRole(roleID);
      if (data.code === '000000') {
        this.dialogFormVisible = true;
        this.EditForm = data.data;
      }
    },
    async handleEdited() {
      const { data } = await UpdateRole(this.EditForm);
      if (data.code === '000000') {
        this.dialogFormVisible = false;
        this.$message.success('角色更新成功');
        this.loadRoles();
      }
    },
    async handleDelete(roleID) {
      const { data } = await DeleteRole(roleID);
      if (data.code === '000000') {
        this.$message.success('角色删除成功');
        this.loadRoles();
      }
    },
  },
};
</script>

<style scoped>
::v-deep .el-table_1_column_5 .cell {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}
::v-deep .el-table_1_column_5 .cell .el-button{
  margin: 0 5px;
}
</style>
